package com.mobzilla.service;

import java.util.Date;

import com.mobzilla.model.Bug;
import com.mobzilla.model.Comment;
import com.mobzilla.util.Constants;
import com.mobzilla.util.Lock;

public class CreateServiceImpl implements CreateService {

	private static final String URL_PREFIX = Constants.BASE_URL + "/bug/";

	private static final String STR_ADD_COMMENT = "{\"is_private\":false,\"creator\":{\"name\":"
			+ "\"$$name$$\"},\"text\":\"$$text$$\",\"creation_time\":\"$$time$$\"}";
			
	private static final String STR_NEW_DEFECT = "{\"priority\":\"$$priority$$\",\"creator\":{\"real_name\":\"$$real_name$$\",\"name\":\"$$name$$\"}," +
				"\"status\":\"$$status$$\",\"last_change_time\":\"$$time$$\",\"is_cc_accessible\":true,\"summary\":\"$$summary$$\"," +
				"\"creation_time\":\"$$time$$\",\"platform\":\"$$platform$$\",\"severity\":\"$$severity$$\"," +
				"\"version\":\"$$version$$\",\"component\":\"$$component$$\",\"is_creator_accessible\":true,\"is_confirmed\":true," +
				"\"target_milestone\":\"---\",\"classification\":\"Unclassified\",\"product\":\"$$product$$\",\"op_sys\":\"$$op_sys$$\"}";

	@Override
	public boolean addComment(int bugId, Comment comment, String userName,
			String password) {

		if (bugId <= 0 || comment == null || userName == null
				|| password == null) {
			return false;
		}

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.append(bugId).append("/comment?username=").append(userName);
		builder.append("&password=").append(password);


		String addComment =STR_ADD_COMMENT.replace("$$name$$", comment.getCreator().getName());
		addComment = addComment.replace("$$text$$", comment.getText());
		addComment = addComment.replace("$$time$$", new Date().toLocaleString());
		
		return postJsonStringAsync(builder.toString(), addComment);

	}
	
	@Override
	public boolean createNewDefect(Bug bug, String userName, String password) {

		if (bug == null || userName == null
				|| password == null) {
			return false;
		}

		StringBuilder builder = new StringBuilder(URL_PREFIX);
		builder.deleteCharAt(builder.length()-1);
		builder.append("?username=").append(userName);
		builder.append("&password=").append(password);


		String newBug = STR_NEW_DEFECT.replace("$$priority$$", bug.getPriority());
		newBug = newBug.replace("$$real_name$$", bug.getCreator().getRealName());
		newBug = newBug.replace("$$name$$", bug.getCreator().getName());
		newBug = newBug.replace("$$summary$$", bug.getSummary());
		newBug = newBug.replace("$$platform$$", bug.getPlatform());
		newBug = newBug.replace("$$severity$$", bug.getSeverity());
		newBug = newBug.replace("$$version$$", bug.getVersion());
		newBug = newBug.replace("$$component$$", bug.getComponent());
		newBug = newBug.replace("$$op_sys$$", bug.getOpSys());
		newBug = newBug.replace("$$product$$", bug.getProduct());
		newBug = newBug.replace("$$status$$", bug.getStatus());
		newBug = newBug.replace("$$time$$", new Date().toLocaleString());
		newBug = newBug.replace("$$time$$", new Date().toLocaleString());
		
		return postJsonStringAsync(builder.toString(), newBug);

	}

	private boolean postJsonStringAsync(String url, String jsonString) {
		Lock<String> lock = new Lock<String>();
		HttpRequestPostAsync postAsync = new HttpRequestPostAsync(url, lock,
				jsonString);
		postAsync.execute();

		while (!lock.isDataSet()) {
			// Do Nothing
		}

		if (lock.getData() == null) {
			return false;
		}
		return true;
	}

}
